import React from 'react';
import { IndexRoute, Route } from 'react-router';
// import { isLoaded as isAuthLoaded, load as loadAuth } from 'redux/modules/auth';
import {
  App,
  Chat,
  Home,
  Widgets,
  About,
  Login,
  LoginSuccess,
  Survey,
  NotFound,
  ClassesList,
  ClassesItem,
  AuthLayout,
  AdminLayout,
  WebLayout,
  Apps
} from 'containers';

export default (store) => {
  const requireLogin = (nextState, replace, cb) => {
    function checkAuth() {
      const { auth: { user } } = store.getState();
      if (!user) {
        // oops, not logged in, so can't be here!
        replace('/login');
      }
      cb();
    }
    checkAuth();
  };

  /**
   * Please keep routes in alphabetical order
   */
  return (
    <Route path="/" component={App}>
      <Route component={WebLayout}>
        <IndexRoute component={Home}/>
        <Route path="about" component={About}/>
        <Route path="survey" component={Survey}/>
        <Route path="widgets" component={Widgets}/>
      </Route>
      <Route component={AdminLayout}>
        <Route path="admin" component={Apps}/>
        <Route path="chat" component={Chat} onEnter={requireLogin}/>
        <Route path="loginSuccess" component={LoginSuccess}/>
        <Route path="classes/:className" component={ClassesList} />
        <Route path="classes/:className/:objectId" component={ClassesItem} />
      </Route>
      <Route component={AuthLayout}>
         <Route path="login" component={Login}/>
      </Route>
      <Route path="*" component={NotFound} status={404} />
    </Route>
  );
};
